package com.ali.juejin.widget

import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.ali.juejin.BR
import com.ali.juejin.R
import com.ali.juejin.databinding.ActivityLoginBinding
import com.ali.juejin.model.login.LoginModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.animator.TranslateAnimator
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.util.XPopupUtils

/**
 * Description: 宽高撑满的全屏弹窗使用databinding
 * Create by lxj, at 2019/2/1
 */
class LoginPopView(context: Context) : CenterPopupView(context) {

    var argbEvaluator = ArgbEvaluator()
    override fun getMaxWidth(): Int {
        return 0
    }

    override fun initPopupContent() {
        popupInfo.hasShadowBg = false
        val bind = DataBindingUtil.inflate<ActivityLoginBinding>(LayoutInflater.from(context), R.layout.activity_login, centerPopupContainer, false)
        val contentView = bind.root
        val vm = LoginModel()
        bind.setVariable(BR.model,vm)
        bind.executePendingBindings()
        val params = contentView.layoutParams as LayoutParams
        params.gravity = Gravity.CENTER
        centerPopupContainer.addView(contentView, params)
        popupContentView.translationX = popupInfo.offsetX.toFloat()
        popupContentView.translationY = popupInfo.offsetY.toFloat()
        XPopupUtils.applyPopupSize(
            popupContentView as ViewGroup,
            maxWidth,
            maxHeight
        )
    }

    override fun onNavigationBarChange(show: Boolean) {
        if (!show) {
            applyFull()
            popupContentView.setPadding(0, 0, 0, 0)
        } else {
            applySize(true)
        }
    }

    override fun applySize(isShowNavBar: Boolean) {
        val rotation =
            (context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).defaultDisplay
                .rotation
        val contentView = popupContentView
        val params =
            contentView.layoutParams as LayoutParams
        params.gravity = Gravity.TOP
        contentView.layoutParams = params
        val actualNabBarHeight =
            if (isShowNavBar || XPopupUtils.isNavBarVisible(dialog.window)) XPopupUtils.getNavBarHeight() else 0
        if (rotation == 0) {
            contentView.setPadding(
                contentView.paddingLeft,
                contentView.paddingTop,
                contentView.paddingRight,
                actualNabBarHeight
            )
        } else if (rotation == 1 || rotation == 3) {
            contentView.setPadding(
                contentView.paddingLeft,
                contentView.paddingTop,
                contentView.paddingRight,
                0
            )
        }
    }

    var paint: Paint? = Paint()
    var shadowRect: Rect? = null
    var currColor = Color.TRANSPARENT
    override fun dispatchDraw(canvas: Canvas) {
        super.dispatchDraw(canvas)
        if (popupInfo.hasStatusBarShadow) {
            paint!!.color = currColor
            shadowRect = Rect(
                0,
                0,
                XPopupUtils.getWindowWidth(context),
                XPopupUtils.getStatusBarHeight()
            )
            canvas.drawRect(shadowRect!!, paint!!)
        }
    }

    override fun doShowAnimation() {
        super.doShowAnimation()
        doStatusBarColorTransform(true)
    }

    override fun doDismissAnimation() {
        super.doDismissAnimation()
        doStatusBarColorTransform(false)
    }

    fun doStatusBarColorTransform(isShow: Boolean) {
        if (popupInfo.hasStatusBarShadow) {
            //状态栏渐变动画
            val animator = ValueAnimator.ofObject(
                argbEvaluator,
                if (isShow) Color.TRANSPARENT else XPopup.statusBarShadowColor,
                if (isShow) XPopup.statusBarShadowColor else Color.TRANSPARENT
            )
            animator.addUpdateListener { animation ->
                currColor = animation.animatedValue as Int
                postInvalidate()
            }
            animator.setDuration(XPopup.getAnimationDuration().toLong()).start()
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        paint = null
    }

    override fun getPopupAnimator(): PopupAnimator {
        return TranslateAnimator(popupContentView, PopupAnimation.TranslateFromBottom)
    }
}